package com.ddxz.tool.core.security;

import java.util.Base64;

/**
 * @author 朱宗刚
 * @version 1.0.0
 * @description com.sdlyzzg.dubbo.com.casic.common.tools.security.Base64Util
 * @date 2020/2/3
 */
public class Base64Util {
    private Base64Util() {
    }

    public static String encodeBase64(String context) {
        return encode(context.getBytes());
    }

    public static String encode(byte[] context) {
        return Base64.getEncoder().encodeToString(context);
    }

    public static String encode(Byte[] context) {
        byte[] tmp = new byte[context.length];
        for (int i = 0; i < context.length; ++i) {
            tmp[i] = context[i];
        }

        return Base64.getEncoder().encodeToString(tmp);
    }

    public static String decodeBase64(String base64) {
        return new String(Base64.getDecoder().decode(base64));
    }

    public static byte[] decode(String base64) {
        return Base64.getDecoder().decode(base64);
    }

    public static Byte[] decode(CharSequence base64) {
        byte[] tmp = Base64.getDecoder().decode(base64.toString());
        Byte[] rst = new Byte[tmp.length];
        for (int i = 0; i < tmp.length; ++i) {
            rst[i] = tmp[i];
        }

        return rst;
    }
}
